package org.asiaark.developerstudio.commons.lang.web;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletRequest;

import org.asiaark.developerstudio.commons.lang.StringUtils;
import org.asiaark.developerstudio.commons.lang.math.NumberUtils;

/**
 * 获取请求工具
 * 
 * @author wsf
 * @version 201201
 * 
 */
public class RequestUtils {

	private ServletRequest request;

	public RequestUtils(ServletRequest request) {
		this.request = request;
	}

	public ServletRequest getRequest() {
		return request;
	}

	/**
	 * 获取boolean型参数
	 * 
	 * @param name
	 * @return
	 */
	public boolean getBoolean(String name) {
		return Boolean.parseBoolean(getString(name));
	}

	/**
	 * 获取boolean型参数
	 * 
	 * @param name
	 * @param defaultValue
	 * @return
	 */
	public boolean getBoolean(String name, boolean defaultValue) {
		if (StringUtils.isEmpty(getString(name)))
			return defaultValue;
		else
			return getBoolean(name);
	}

	/**
	 * 获取byte型参数
	 * 
	 * @param name
	 * @return
	 */
	public byte getByte(String name) {
		return Byte.parseByte(name);
	}

	/**
	 * 获取byte型参数
	 * 
	 * @param name
	 * @param defaultValue
	 * @return
	 */
	public byte getByte(String name, byte defaultValue) {
		if (StringUtils.isEmpty(getString(name)))
			return defaultValue;
		else
			return getByte(name);
	}

	/**
	 * 获取int型参数
	 * 
	 * @param name
	 * @return
	 */
	public int getInt(String name) {
		return NumberUtils.toInt(request.getParameter(name));
	}

	/**
	 * 获取int型参数
	 * 
	 * @param name
	 * @param defaultValue
	 * @return
	 */
	public int getInt(String name, int defaultValue) {
		return NumberUtils.toInt(request.getParameter(name), defaultValue);
	}

	/**
	 * 获取long型参数
	 * 
	 * @param name
	 * @return
	 */
	public long getLong(String name) {
		return NumberUtils.toLong(request.getParameter(name));
	}

	/**
	 * 获取long型参数
	 * 
	 * @param name
	 * @param defaultValue
	 * @return
	 */
	public long getLong(String name, int defaultValue) {
		return NumberUtils.toLong(request.getParameter(name), defaultValue);
	}

	/**
	 * 获取float型参数
	 * 
	 * @param name
	 * @return
	 */
	public float getFloat(String name) {
		return NumberUtils.toFloat(request.getParameter(name));
	}

	/**
	 * 获取float型参数
	 * 
	 * @param name
	 * @param defaultValue
	 * @return
	 */
	public float getFloat(String name, int defaultValue) {
		return NumberUtils.toFloat(request.getParameter(name), defaultValue);
	}

	/**
	 * 获取double型参数
	 * 
	 * @param name
	 * @return
	 */
	public double getDouble(String name) {
		return NumberUtils.toDouble(request.getParameter(name));
	}

	/**
	 * 获取double型参数
	 * 
	 * @param name
	 * @param defaultValue
	 * @return
	 */
	public double getDouble(String name, int defaultValue) {
		return NumberUtils.toDouble(request.getParameter(name), defaultValue);
	}

	/**
	 * 获取String型参数
	 * 
	 * @param name
	 * @return
	 */
	public String getString(String name) {
		return request.getParameter(name);
	}

	/**
	 * 获取String型参数
	 * 
	 * @param name
	 * @param defaultValue
	 * @return
	 */
	public String getString(String name, String defaultValue) {
		return StringUtils.isEmpty(getString(name)) ? defaultValue
				: getString(name);
	}

	/**
	 * 获取Date型参数
	 * 
	 * @param name
	 * @return
	 * @throws ParseException
	 */
	public Date getDate(String name) throws ParseException {
		return getDate(name, "yyyy-MM-dd");
	}

	/**
	 * 获取Date型参数
	 * 
	 * @param name
	 * @param defaultValue
	 * @return
	 * @throws ParseException
	 */
	public Date getDate(String name, Date defaultValue) throws ParseException {
		if (StringUtils.isEmpty(getString(name)))
			return defaultValue;
		else
			return getDate(name, "yyyy-MM-dd");
	}

	/**
	 * 获取Date型参数
	 * 
	 * @param name
	 * @param format
	 * @return
	 * @throws ParseException
	 */
	public Date getDate(String name, String format) throws ParseException {
		return new SimpleDateFormat(format).parse(getString(name));
	}

	/**
	 * 获取Date型参数
	 * 
	 * @param name
	 * @param format
	 * @param defaultValue
	 * @return
	 * @throws ParseException
	 */
	public Date getDate(String name, String format, Date defaultValue)
			throws ParseException {
		if (StringUtils.isEmpty(getString(name)))
			return defaultValue;
		else
			return getDate(name, format);
	}

	/**
	 * 返回map数据
	 * 
	 * @return
	 */
	public Map<String, String[]> toMap() {
		return request.getParameterMap();
	}

	/**
	 * 返回经过处理的map
	 * 
	 * @return
	 */
	public Map<String, Object> toSmartMap() {
		Map<String, Object> rst = new HashMap<String, Object>();
		Enumeration<String> e = request.getParameterNames();
		while (e.hasMoreElements()) {
			String name = e.nextElement();
			String[] values = request.getParameterValues(name);
			if (values == null)
				rst.put(name, null);
			else if (values.length == 1)
				rst.put(name, values[0]);
			else
				rst.put(name, values);
		}
		return rst;
	}

	public static void main(String[] args) {

	}

}
